<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

aos_header( 'Buddies' );

$query = "SELECT rating_points FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$rating_points = $row2[ 'rating_points' ];
$query = "SELECT count(*) FROM buddy WHERE ( user_id1 = " . $_SESSION[ 'user_id' ] . " OR user_id2 = " . $_SESSION[ 'user_id' ] . " ) AND accept1 = 1 AND accept2 = 1";
$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$buddies = $row2[ 'count(*)' ];
$query = "SELECT count(*) FROM buddy WHERE ( user_id1 = " . $_SESSION[ 'user_id' ] . " OR user_id2 = " . $_SESSION[ 'user_id' ] . " ) AND ( accept2 = 0 OR accept1 = 0 )";
$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$invites = $row2[ 'count(*)' ];
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Buddies' );
?> 
					</div>
					<br />
					<div class="blok">
						<?php transprint( 'Buddies' ); ?>: <?php print( number_format( $buddies ) ); ?><br />
						<?php transprint( 'Invites' ); ?>: <?php print( number_format( $invites ) ); ?><br />
						<?php transprint( 'My rating points' ); ?>: <?php print( number_format( floor( $rating_points ) ) ); ?>
					</div>
					<br />
<?php
if( isset( $_SESSION[ 'buddylist_status' ] ) ) {
	print( "<div class=\"blok\">" . translate( 'Status' ) . ": " . $_SESSION[ 'buddylist_status' ] . "</div><br />" );
	unset( $_SESSION[ 'buddylist_status' ] );
}
$query = "SELECT * " .
		"FROM buddy " .
		"WHERE ( user_id1 = " . $_SESSION[ 'user_id' ] . " OR user_id2 = " . $_SESSION[ 'user_id' ] . " ) AND accept1 = 1 AND accept2 = 1";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
if( $num > 0 ) {
    print( "<form action=\"update_list.php\" method=\"post\">\r\n" );
    print( "<table><th colspan=\"10\">" . translate( 'Buddies' ) . "</th>\r\n" );
    print( "<tr><td>" . translate( 'Username' ) . "</td><td>" . translate( 'Buddies' ) . "</td><td>" . translate( 'Ruler name' ) . "</td><td>" . translate( 'Last online' ) . "</td><td>" . translate( 'Rating points' ) . "</td><td>" . translate( 'Show my ruler' ) . "</td><td>" . translate( 'Show my activity' ) . "</td><td>" . translate( 'Show my e-mail' ) . "</td><td>" . translate( 'Show my info' ) . "</td><td>" . translate( 'Drop buddy' ) . "</td></tr>\r\n" );
    while ( $i < $num ) {
        if( $i % 2 == 0 ) {
            print "<tr id=\"even\">";
        }
        else {
            print "<tr>";
        }
        $ruler_checked = "";
        $turn_checked = "";
        $email_checked = "";
        $info_checked = "";
        
        $link = false;
        $other_user_id = mysql_result( $result, $i, "user_id1" );
        $number = 1;
		if( mysql_result( $result, $i, "email1" ) == 1 || mysql_result( $result, $i, "info1" ) == 1 ) {
			$link = true;
		}
        if( $other_user_id == $_SESSION[ 'user_id' ] ) {
        	$link = false;
			if( mysql_result( $result, $i, "email2" ) == 1 || mysql_result( $result, $i, "info2" ) == 1 ) {
				$link = true;
			}
        	$other_user_id = mysql_result( $result, $i, "user_id2" );
        	$number = 2;
        }
		$query = "SELECT username FROM users WHERE user_id = $other_user_id";
		$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		print( "<td><a href=\"buddy.php?id=" . mysql_result( $result, $i, "buddy_id" ) . "\">" . $row2[ 'username' ] . "</a></td>" );
		
		$query3 = "SELECT count(*) FROM buddy WHERE ( user_id1 = $other_user_id OR user_id2 = $other_user_id ) AND accept1 = 1 AND accept2 = 1";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		print( "<td align=\"right\">" . number_format( $row3[ 'count(*)' ] ) . "</td>" ); 
		if( $number == 1 ) {
			if( mysql_result( $result, $i, "ruler1" ) == 1 ) {
				$query3 = "SELECT name FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'name' ] ) ) {
					print( "<td><a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">" . $row3[ 'name' ] . "</a></td>" );
				}
				else {
					print( "<td>" . translate( 'not signed up' ) . "</td>" );
				}
			}
			else {
				print( "<td>" . translate( 'hidden' ) . "</td>" );
			}
			
			if( mysql_result( $result, $i, "turn1" ) == 1 ) {
				$query3 = "SELECT active_turn FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'active_turn' ] ) ) {
					$current_turn = getCurrentTurn();
					if( $row3[ 'active_turn' ] == $current_turn ) {
						print( "<td>" . translate( 'This game day' ) . "</td>" );
					}
					else {
						print( "<td>" . number_format( $current_turn - $row3[ 'active_turn' ] ) . " " . translate( 'game days ago' ) . "</a></td>" );
					}
				}
				else {
					print( "<td>" . translate( 'unknown' ) . "</td>" );
				}
			}
			else {
				print( "<td>" . translate( 'hidden' ) . "</td>" );
			}
			
			if( mysql_result( $result, $i, "ruler2" ) == 1 ) {
				$ruler_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "email2" ) == 1 ) {
				$email_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "info2" ) == 1 ) {
				$info_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "turn2" ) == 1 ) {
				$turn_checked = "checked=\"checked\"";
			}
		}
		else {
			if( mysql_result( $result, $i, "ruler2" ) == 1 ) {
				$query3 = "SELECT name FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'name' ] ) ) {
					print( "<td><a href=\"../overview/ruler.php?name=" . $row3[ 'name' ] . "\">" . $row3[ 'name' ] . "</a></td>" );
				}
				else {
					print( "<td>" . translate( 'resigned' ) . "</td>" );
				}
			}
			else {
				print( "<td>" . translate( 'hidden' ) . "</td>" );
			}
			if( mysql_result( $result, $i, "turn2" ) == 1 ) {
				$query3 = "SELECT active_turn FROM ruler WHERE user_id = $other_user_id";
				$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
				if( isset( $row3[ 'active_turn' ] ) ) {
					$current_turn = getCurrentTurn();
					if( $row3[ 'active_turn' ] == $current_turn ) {
						print( "<td>" . translate( 'This game day' ) . "</td>" );
					}
					else {
						print( "<td>" . number_format( $current_turn - $row3[ 'active_turn' ] ) . " " . translate( 'game days ago' ) . "</a></td>" );
					}
				}
				else {
					print( "<td>" . translate( 'unknown' ) . "</td>" );
				}
			}
			else {
				print( "<td>" . translate( 'hidden' ) . "</td>" );
			}
			if( mysql_result( $result, $i, "ruler1" ) == 1 ) {
				$ruler_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "email1" ) == 1 ) {
				$email_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "info1" ) == 1 ) {
				$info_checked = "checked=\"checked\"";
			}
			if( mysql_result( $result, $i, "turn1" ) == 1 ) {
				$turn_checked = "checked=\"checked\"";
			}
		}
		if( mysql_result( $result, $i, "ruler1" ) == 1 && mysql_result( $result, $i, "ruler2" ) == 1 ) {
			$query3 = "SELECT rating_points FROM users WHERE user_id = $other_user_id";
			$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			print( "<td align=\"right\">" . number_format( floor( $row3[ 'rating_points' ] ) ) . "</td>" );
		}
		else {
			print( "<td>" . translate( 'hidden' ) . "</td>" );
		}
		
		print( "<td><input type=\"checkbox\" $ruler_checked name=\"show_ruler_" . $other_user_id . "\" /></td>" );
		print( "<td><input type=\"checkbox\" $turn_checked name=\"show_turn_" . $other_user_id . "\" /></td>" );
		print( "<td><input type=\"checkbox\" $email_checked name=\"show_email_" . $other_user_id . "\" /></td>" );
		print( "<td><input type=\"checkbox\" $info_checked name=\"show_info_" . $other_user_id . "\" /></td>" );
		print( "<td><input type=\"hidden\" name=\"$i\" value=\"$other_user_id\" /><input type=\"checkbox\" name=\"drop_buddy_" . $other_user_id . "\" /></td>" );
		print( "</tr>\r\n" );
        $i ++;
    }
    ?>
			    	</table>
					<input type="submit" value="<?php transprint( 'update' ); ?>" />
			    </form><br />
	<?php
}
else {
	print( "<div class=\"blok\">" . translate( 'You don\'t have any buddies yet, invite some!' ) );
    print( "</div>\r\n" );
}
?>
		    <br />
<?php			    	
print( "<div class=\"blok\">" . translate( 'Invite a new buddy' ) . ": <form method=\"post\" action=\"invite.php\">\r\n" );
print( "<input type=\"text\" name=\"ruler_name\" /> \r\n" .
	  "<input type=\"submit\" value=\"submit\" /></form><br /><br />\r\n" );
print( translate( 'You can change your info message and e-mail address' ) . " <a href=\"../overview/settings.php\">" . translate( 'here' ) . "</a>.<br />" );
$query3 = "SELECT gameurl FROM game";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
print( translate( 'To <strong>tell a friend</strong> about Ages of Strife click' ) . " <a href=\"mailto:?subject=Ages of Strife&body=Hi,%0A%0a" . translate( 'I\'ve been playing this browser-based strategy game, Ages of Strife.' ) . "%0A" . translate( 'Perhaps you would like to join me to conquer the world (only in the game of course).' ) . "%0A" . translate( 'Check it out at' ) . " " . $row3[ 'gameurl' ] . "%0A%0a" . translate( 'Hope to see you in game!' ) . "\">" . translate( 'here' ) . "</a>.<br />" );
print( "</div><br />\r\n" );

$query = "SELECT * FROM buddy WHERE ( user_id1 = " . $_SESSION[ 'user_id' ] . " OR user_id2 = " . $_SESSION[ 'user_id' ] . " ) AND ( accept1 = 0 OR accept2 = 0 )";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
if( $num > 0 ) {
    print( "<table id=\"twothird\"><th colspan=\"2\">" . translate( 'Buddy invites' ) . "</th>\r\n" );
    print( "<tr><td>" . translate( 'User/ruler' ) . "</td><td>" . translate( 'Action' ) . "</td></tr>\r\n" );
    while ( $i < $num ) {
    	if( $i % 2 == 0 ) {
            print "<tr id=\"even\">";
        }
        else {
            print "<tr>";
        }
        
        $other_user_id = mysql_result( $result, $i, "user_id1" );
        $number = 1;
        if( $other_user_id == $_SESSION[ 'user_id' ] ) {
        	$other_user_id = mysql_result( $result, $i, "user_id2" );
        	$number = 2;
        }
        if( mysql_result( $result, $i, "new" ) == 1 ) {
			$query = "SELECT name FROM ruler WHERE user_id = $other_user_id";
			$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			if( isset( $row2[ 'name' ] ) ) {
				$name = $row2[ 'name' ];
			}
			else {
				$name = translate( 'Hmm, this should not happen!' );
			}
        }
        else {
			$query = "SELECT username FROM users WHERE user_id = $other_user_id";
			$result2 = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
			$name = $row2[ 'username' ];
        }
		print( "<td>" . $name . "</td>" );
		if( $number == 1 ) {
			if( mysql_result( $result, $i, "accept1" ) == 1 ) {
				print( "<td><form method=\"post\" action=\"buddy_action.php\"><input type=\"hidden\" name=\"type\" value=\"1\" /><input type=\"hidden\" name=\"user_id\" value=\"$other_user_id\" /><input type=\"submit\" name=\"accept\" value=\"" . translate( 'accept invite' ) . "\" /></form></td>" );
			}
			else {
				print( "<td><form method=\"post\" action=\"buddy_action.php\"><input type=\"hidden\" name=\"type\" value=\"2\" /><input type=\"hidden\" name=\"user_id\" value=\"$other_user_id\" /><input type=\"submit\" name=\"accept\" value=\"" . translate( 'cancel invitation' ) . "\" /></form></td>" );
			}
		}
		else {
			if( mysql_result( $result, $i, "accept2" ) == 1 ) {
				print( "<td><form method=\"post\" action=\"buddy_action.php\"><input type=\"hidden\" name=\"type\" value=\"1\" /><input type=\"hidden\" name=\"user_id\" value=\"$other_user_id\" /><input type=\"submit\" name=\"accept\" value=\"" . translate( 'accept invite' ) . "\" /></form></td>" );
			}
			else {
				print( "<td><form method=\"post\" action=\"buddy_action.php\"><input type=\"hidden\" name=\"type\" value=\"2\" /><input type=\"hidden\" name=\"user_id\" value=\"$other_user_id\" /><input type=\"submit\" name=\"accept\" value=\"" . translate( 'cancel invitation' ) . "\" /></form></td>" );
			}
		}
		print( "</tr>\r\n" );
        $i ++;
    }
?>
		    	</table>
			</form>
<?php
}
else {
	print( "<div class=\"blok\">" . translate( 'You don\'t have any outstanding buddy invites.' ) );
    print( "</div>\r\n" );
}
aos_footer();
?>